<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="通道编号" prop="channelId">
        <el-input
          v-model="queryParams.channelId"
          placeholder="请输入通道编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="通道名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入通道名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备ID" prop="deviceId">
        <el-input
          v-model="queryParams.deviceId"
          placeholder="请输入设备ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['iot:channel:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['iot:channel:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-refresh"
          size="mini"
          @click="handleSync"
          v-hasPermi="['iot:channel:sync']"
        >同步国标</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="channelList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="通道编号" align="center" prop="channelId" />
      <el-table-column label="通道名称" align="center" prop="name" />
      <el-table-column label="自定义名称" align="center" prop="customName" />
      <el-table-column label="设备ID" align="center" prop="deviceId" />
      <el-table-column label="IP地址" align="center" prop="ipAddress" />
      <el-table-column label="端口号" align="center" prop="port" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['iot:channel:edit']"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改通道对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="通道编号" prop="channelId">
              <el-input v-model="form.channelId" placeholder="请输入通道编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="通道名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入通道名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="自定义名称" prop="customName">
              <el-input v-model="form.customName" placeholder="请输入自定义名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备ID" prop="deviceId">
              <el-select v-model="form.deviceId" placeholder="请选择设备" style="width: 100%">
                <el-option
                  v-for="device in deviceOptions"
                  :key="device.deviceCode"
                  :label="device.deviceName"
                  :value="device.deviceCode"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="IP地址" prop="ipAddress">
              <el-input v-model="form.ipAddress" placeholder="请输入IP地址" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="端口号" prop="port">
              <el-input-number v-model="form.port" :min="1" :max="65535" placeholder="请输入端口号" style="width: 100%" controls-position="right" :controls="false" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio :label="true">启用</el-radio>
                <el-radio :label="false">禁用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="云台类型" prop="ptzType">
              <el-select v-model="form.ptzType" placeholder="请选择云台类型" style="width: 100%">
                <el-option label="未知" :value="0" />
                <el-option label="球机" :value="1" />
                <el-option label="半球" :value="2" />
                <el-option label="固定枪机" :value="3" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="经度" prop="longitude">
              <el-input-number v-model="form.longitude" :precision="6" :step="0.000001" placeholder="请输入经度" style="width: 100%" controls-position="right" :controls="false" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="纬度" prop="latitude">
              <el-input-number v-model="form.latitude" :precision="6" :step="0.000001" placeholder="请输入纬度" style="width: 100%" controls-position="right" :controls="false" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listChannel, getChannel, addChannel, updateChannel, syncChannel } from "@/api/iot/channel";
import { listCameraDevice } from "@/api/iot/device";

export default {
  name: "Channel",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 通道表格数据
      channelList: [],
      // 弹出框标题
      title: "",
      // 是否显示弹出框
      open: false,
      // 设备选项
      deviceOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        channelId: null,
        name: null,
        deviceId: null,
        status: null
      },
      // 表单参数
      form: {
        id: undefined,
        channelId: undefined,
        name: undefined,
        customName: undefined,
        deviceId: undefined,
        ipAddress: undefined,
        port: undefined,
        status: true,
        ptzType: 0,
        longitude: undefined,
        latitude: undefined
      },
      // 表单校验
      rules: {
        channelId: [
          { required: true, message: "通道编号不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "通道名称不能为空", trigger: "blur" }
        ],
        deviceId: [
          { required: true, message: "设备不能为空", trigger: "change" }
        ],
        ipAddress: [
          { pattern: /^(\d{1,3}\.){3}\d{1,3}$/, message: "请输入正确的IP地址", trigger: "blur" }
        ],
        port: [
          { type: "number", min: 1, max: 65535, message: "端口号范围为1-65535", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDeviceOptions();
  },
  methods: {
    /** 查询通道列表 */
    getList() {
      this.loading = true;
      listChannel(this.queryParams).then(response => {
        this.channelList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 获取设备选项 */
    getDeviceOptions() {
      listCameraDevice().then(response => {
        this.deviceOptions = response.rows;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        channelId: undefined,
        name: undefined,
        customName: undefined,
        deviceId: undefined,
        ipAddress: undefined,
        port: undefined,
        status: true,
        ptzType: 0,
        longitude: undefined,
        latitude: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加通道";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getChannel(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改通道";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id) {
            updateChannel(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addChannel(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 同步国标操作 */
    handleSync() {
      this.$modal.confirm('是否确认同步国标通道数据？').then(() => {
        this.loading = true;
        return syncChannel();
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("同步成功");
      }).catch(() => {
        this.loading = false;
      });
    }
  }
};
</script>

<style scoped>
/* 去除输入框的上下箭头 */
:deep(.el-input-number .el-input__inner) {
  text-align: left;
  padding-left: 12px;
}
</style> 